﻿using System;
using System.Data;
using System.Collections.Generic;
using CrazyTalk.Caching;
using CrazyTalk.DALFactory;
using CrazyTalk.IDAL;
using CrazyTalk.Models;
using CrazyTalk.Common;
using DataCache = CrazyTalk.DALFactory.DataCache;

namespace CrazyTalk.BLL
{
	/// <summary>
	/// UserPermissionListBLL
	/// </summary>
	public class UserPermissionListBLL
	{
        private static UserPermissionListBLL _instance;
        private static readonly object SyncLock = new object();
        public static UserPermissionListBLL Instance
        {
            get
            {
                lock (SyncLock)
                {
                    if (_instance == null)
                    {
                        _instance = new UserPermissionListBLL();
                    }
                }
                return _instance;
            }
        }
		private readonly IUserPermissionListDAL dal=DataAccess.Get<IUserPermissionListDAL>();
		public UserPermissionListBLL()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
			return dal.GetMaxId();
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(string UserId,int MenuID)
		{
			return dal.Exists(UserId,MenuID);
		}

		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int  Add(UserPermissionList model)
		{
			return dal.Add(model);
		}

		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(UserPermissionList model)
		{
			return dal.Update(model);
		}

        /// <summary>
        /// 更新用户的所有权限
        /// </summary>
        /// <param name="uid">需要更新权限的用户ID</param>
        /// <param name="list">用户的新权限列表</param>
        /// <returns></returns>
        public bool UpdateList(string uid, List<Strs> list)
        {
            bool sign = dal.UpdateList(uid, list);

            if (sign)
            {
                DefaultCache cache = DefaultCache.GetCacheService();
                string cacheKey = CacheKeys.Permission_List_User + uid;
                cache.RemoveObject(cacheKey);
            }

            return sign;
        }
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int RecordId)
		{
			
			return dal.Delete(RecordId);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(string UserId,int MenuID)
		{
			
			return dal.Delete(UserId,MenuID);
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string RecordIdlist )
		{
			return dal.DeleteList(RecordIdlist );
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public UserPermissionList GetModel(int RecordId)
		{
			
			return dal.GetModel(RecordId);
		}

		/// <summary>
		/// 得到一个对象实体，从缓存中
		/// </summary>
		public UserPermissionList GetModelByCache(int RecordId)
		{
			
			string CacheKey = "UserPermissionListModel-" + RecordId;
			object objModel = DataCache.GetCache(CacheKey);
			if (objModel == null)
			{
				try
				{
					objModel = dal.GetModel(RecordId);
					if (objModel != null)
					{
						int ModelCache = ConfigHelper.GetConfigInt("ModelCache");
						DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
					}
				}
				catch{}
			}
			return (UserPermissionList)objModel;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(List<string> fields, List<object> values)
		{
			return dal.GetList(fields, values);
		}
		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,List<string> fields, List<object> values,string filedOrder)
		{
			return dal.GetList(Top,fields, values,filedOrder);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<UserPermissionList> GetModelList(List<string> fields, List<object> values)
		{
			DataSet ds = dal.GetList(fields, values);
			return DataTableToList(ds.Tables[0]);
		}
		/// <summary>
		/// 获得数据列表
		/// </summary>
		public List<UserPermissionList> DataTableToList(DataTable dt)
		{
			List<UserPermissionList> modelList = new List<UserPermissionList>();
			int rowsCount = dt.Rows.Count;
			if (rowsCount > 0)
			{
				UserPermissionList model;
				for (int n = 0; n < rowsCount; n++)
				{
					model = new UserPermissionList();
					if(dt.Rows[n]["RecordId"]!=null && dt.Rows[n]["RecordId"].ToString()!="")
					{
						model.RecordId=int.Parse(dt.Rows[n]["RecordId"].ToString());
					}
					if(dt.Rows[n]["UserId"]!=null && dt.Rows[n]["UserId"].ToString()!="")
					{
					model.UserId=dt.Rows[n]["UserId"].ToString();
					}
					if(dt.Rows[n]["MenuID"]!=null && dt.Rows[n]["MenuID"].ToString()!="")
					{
						model.MenuID=int.Parse(dt.Rows[n]["MenuID"].ToString());
					}
					if(dt.Rows[n]["Type"]!=null && dt.Rows[n]["Type"].ToString()!="")
					{
						model.Type=int.Parse(dt.Rows[n]["Type"].ToString());
					}
					if(dt.Rows[n]["CreateTime"]!=null && dt.Rows[n]["CreateTime"].ToString()!="")
					{
						model.CreateTime=DateTime.Parse(dt.Rows[n]["CreateTime"].ToString());
					}
					if(dt.Rows[n]["EditTime"]!=null && dt.Rows[n]["EditTime"].ToString()!="")
					{
						model.EditTime=DateTime.Parse(dt.Rows[n]["EditTime"].ToString());
					}
					modelList.Add(model);
				}
			}
			return modelList;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetAllList()
		{
			return GetList(null,null);
		}

		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public int GetRecordCount(List<string> fields, List<object> values)
		{
			return dal.GetRecordCount(fields, values);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(List<string> fields, List<object> values, string orderby, int startIndex, int endIndex)
		{
			return dal.GetListByPage( fields, values,  orderby,  startIndex,  endIndex);
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		//public DataSet GetList(int PageSize,int PageIndex,List<string> fields, List<object> values)
		//{
			//return dal.GetList(PageSize,PageIndex,fields, values);
		//}

		#endregion  Method
	}
}

